package com.mt.channel.service.model;

import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.mt.channel.entity.ChannelCourseEntity;
import com.mt.channel.mapper.ChannelCourseEntityMapper;
import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;

@Component
public class ChannelCourseServiceModel {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ChannelCourseServiceModel.class);

    @Autowired
    private ChannelCourseEntityMapper channelCourseEntityMapper;

    public ServiceResult<List<ChannelCourseEntity>> selectList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        Assert.notNull(this.channelCourseEntityMapper, "Property 'channelCourseEntityMapper' is required.");
        ServiceResult<List<ChannelCourseEntity>> result = new ServiceResult<List<ChannelCourseEntity>>();
        try {
            if (pagerInfo != null) {
                PageHelper.startPage(pagerInfo.getPageIndex(), pagerInfo.getPageSize());
            }
            List<ChannelCourseEntity> result1 = this.channelCourseEntityMapper.selectList(params);
            Object channelBoolean = params.get("channelSelectCourseListBoolean");
            if (channelBoolean != null && (Boolean)channelBoolean) {
                for (ChannelCourseEntity channelCourseEntity : result1) {
                    Integer courseId = channelCourseEntity.getCourseId();
                    params.put("courseId", courseId);
                    Integer channelCoursePayCounts = channelCourseEntityMapper.selectChannelCoursePayCounts(params);
                    channelCourseEntity.setChannelCoursePayCounts(channelCoursePayCounts);
                }
            }
            result.setResult(result1);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelCourseServiceModel][selectList]：query selectList occur exception", e);
        }
        return result;
    }

    public ServiceResult<ChannelCourseEntity> selectInfo(Map<String, Object> params) {
        Assert.notNull(this.channelCourseEntityMapper, "Property 'channelCourseEntityMapper' is required.");
        ServiceResult<ChannelCourseEntity> result = new ServiceResult<ChannelCourseEntity>();
        try {
            result.setResult(this.channelCourseEntityMapper.selectByPrimaryKey(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelCourseServiceModel][selectInfo]：query selectInfo occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> insert(ChannelCourseEntity channelCourseEntity) {
        Assert.notNull(this.channelCourseEntityMapper, "Property 'channelCourseEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelCourseEntityMapper.insert(channelCourseEntity);
            if (save > 0) {
                id = channelCourseEntity.getChannelCourseId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelCourseServiceModel][insert]：query insert occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> updateRelate(ChannelCourseEntity channelCourseEntity) {
        Assert.notNull(this.channelCourseEntityMapper, "Property 'channelCourseEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelCourseEntityMapper.updateByPrimaryKeySelective(channelCourseEntity);
            if (save > 0) {
                id = channelCourseEntity.getChannelCourseId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelCourseServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> updateByPrimaryKeySelective(ChannelCourseEntity channelCourseEntity) {
        Assert.notNull(this.channelCourseEntityMapper, "Property 'channelCourseEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelCourseEntityMapper.updateByPrimaryKeySelective(channelCourseEntity);
            if (save > 0) {
                id = channelCourseEntity.getChannelCourseId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelCourseServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> deleteByPrimaryKey(Integer channelCourseId) {
        Assert.notNull(this.channelCourseEntityMapper, "Property 'channelCourseEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int save = 0;
        try {
            save = this.channelCourseEntityMapper.deleteByPrimaryKey(channelCourseId);
            result.setResult(save);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelCourseServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }
}
